﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using BBEverNote.ViewModels;
using System.Threading;

namespace BBEverNote.Views
{
    /// <summary>
    /// Interaction logic for LoginWnd.xaml
    /// </summary>
    public partial class LoginWnd : Window
    {
        public LoginWnd()
        {
            InitializeComponent();

            this.ContentRendered += new EventHandler(LoginWnd_ContentRendered);
        }

        void LoginWnd_ContentRendered(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            if (_chkAutoLogOn.IsChecked.Value)
            {
                MainViewModel viewModel = this.DataContext as MainViewModel;
                ThreadPool.QueueUserWorkItem(new WaitCallback(threadPool_LogOn), viewModel);

                _btnLogin.IsEnabled = false;
            }
        }

        private void _btnLogin_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            MainViewModel viewModel = this.DataContext as MainViewModel;
            if (viewModel != null)
            {
                viewModel.ENModel.UserName = _txtUserName.Text;
                viewModel.ENModel.Password = _password.Password;
                viewModel.ENModel.IsRememberMe = _chkRemeberMe.IsChecked.Value;
                viewModel.ENModel.IsAutoLogOn = _chkAutoLogOn.IsChecked.Value;

                ThreadPool.QueueUserWorkItem(new WaitCallback(threadPool_LogOn), viewModel);

                _btnLogin.IsEnabled = false;
            }
            else
            {
                //this.DialogResult = true;
                this.Close();
            }
        }

        private void _btnCancel_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            //this.DialogResult = false;
            (this.Owner as Window).Close();
            this.Close();
        }

        private void threadPool_LogOn(object param)
        {
            MainViewModel viewModel = param as MainViewModel;
            if (viewModel != null)
            {
                bool result = viewModel.LogOn();

                if (result)
                {
                    if (App.Current.Dispatcher != null)
                    {
                        System.Windows.Threading.Dispatcher.FromThread(App.Current.Dispatcher.Thread).Invoke((MainViewModel.MethodInvoker)delegate
                        {
                            (this.Owner as Window).Show();
                            //this.DialogResult = true;
                            this.Hide();
                        }
                        , null);
                    }
                    else
                    {
                        (this.Owner as Window).Show();
                        //this.DialogResult = true;
                        this.Hide();
                    } 
                }
                else
                {
                    if (App.Current.Dispatcher != null)
                    {
                        System.Windows.Threading.Dispatcher.FromThread(App.Current.Dispatcher.Thread).Invoke((MainViewModel.MethodInvoker)delegate
                        {
                            this._btnLogin.IsEnabled = true;
                        }
                        , null);
                    }
                    else
                    {
                        this._btnLogin.IsEnabled = true;
                    } 
                }
            }
        }
        
    }
}